// pages/recruit/personal/mytask/mytasksign/attendance/attendance.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageindex: 1,
    signall: false,
    signList: [],
    commitbox: [],
    finishall: false,
    finishList: [],
    finishbox: [],
    jobid: "1111",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    let signList = [{
        userid: "id1",
        jobid: "16874",
        name: "王大拿",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        isSign: true,
        isFinish: false,
        ischecked: true,
      },
      {
        userid: "id2",
        jobid: "16874",
        name: "长贵",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        isSign: false,
        isFinish: false,
        ischecked: false,
      }, {
        userid: "id3",
        jobid: "16874",
        name: "玉田",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        isSign: false,
        isFinish: false,
        ischecked: false,
      }, {
        userid: "id4",
        jobid: "16874",
        name: "小花",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        isSign: false,
        isFinish: false,
        ischecked: false,
      }, {
        userid: "id5",
        jobid: "16874",
        name: "铁柱",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        isSign: false,
        isFinish: false,
        ischecked: false,
      }
    ];
    let finishList = [{
      userid: "id1",
      jobid: "16874",
      name: "王大拿",
      sex: 1,
      age: 27,
      tel: "17755015130",
      education: 1,
      isFinish: true,
      ischecked: true,
    },
    {
      userid: "id2",
      jobid: "16874",
      name: "长贵",
      sex: 1,
      age: 27,
      tel: "17755015130",
      education: 1,
      isFinish: false,
      ischecked: false,
    }, {
      userid: "id3",
      jobid: "16874",
      name: "玉田",
      sex: 1,
      age: 27,
      tel: "17755015130",
      education: 1,
      isFinish: false,
      ischecked: false,
    }, {
      userid: "id4",
      jobid: "16874",
      name: "小花",
      sex: 1,
      age: 27,
      tel: "17755015130",
      education: 1,
      isFinish: false,
      ischecked: false,
    }, {
      userid: "id5",
      jobid: "16874",
      name: "铁柱",
      sex: 1,
      age: 27,
      tel: "17755015130",
      education: 1,
      isFinish: false,
      ischecked: false,
    }
    ]
    that.setData({
      signList: signList,
      finishList: finishList
    })
  },
  changeindex: function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      pageindex: index
    })
  },
  signall: function (e) {//签到全选项
    const that = this;
    that.data.commitbox = [];
    let box = that.data.signList;
    for (let i = 0; i < box.length; i++) {
      if (!that.data.signall) {
        if (!box[i].isSign) {
          box[i].ischecked = true;
          that.data.commitbox.push({
            "userid": box[i].userid,
            "jobid": that.data.jobid
          })
        }
      } else {
        if (!box[i].isSign) {
          box[i].ischecked = false;
          that.data.commitbox = [];
        }
      }
    }
    that.setData({
      signList: that.data.signList,
      signall: !that.data.signall,
      commitbox: that.data.commitbox
    })
  },
  getgroup: function (e) {//签到单选项
    const that = this;
    that.data.commitbox = [];
    let box = that.data.signList;
    let selectbox = e.detail.value;
    if (selectbox.length == box.length) {
      console.log(selectbox)
      for (let i = 0; i < selectbox.length; i++) {
        if (!box[i].isSign) {
          box[selectbox[i]].ischecked = true;
          that.data.commitbox.push({
            "userid": box[i].userid,
            "jobid": that.data.jobid
          })
        }
      }
      that.setData({
        signList: that.data.signList,
        signall: !that.data.signall,
      })
    } else {
      for (let i = 0; i < box.length; i++){
        if (!box[i].isSign){
          box[i].ischecked = false;
        }
      }
      for (let i = 0; i < selectbox.length;i++){
        if (!box[selectbox[i]].isSign) {
          box[selectbox[i]].ischecked = true;
          that.data.commitbox.push({
            "userid": box[selectbox[i]].userid,
            "jobid": that.data.jobid
          })
        }
      }
      that.setData({
        signList: that.data.signList,
        signall: false,
      })
    }
  },
  signThis: function (e) {//签到单个签到
    const that = this;
    let index = e.currentTarget.dataset.index;
    let box = that.data.signList;
    console.log(box[index])
    console.log("这单个开始签到")
  },
  signcommit: function(){//一键提交签到
    const that = this;
    console.log(that.data.commitbox)
  },
  finishall: function (e) {//签退全选项
    const that = this;
    that.data.finishbox=[];
    let box = that.data.finishList;
    for (let i = 0; i < box.length; i++) {
      if (!that.data.finishall) {
        if (!box[i].isFinish) {
          box[i].ischecked = true;
          that.data.finishbox.push({
            "userid": box[i].userid,
            "jobid": that.data.jobid
          })
        }
      } else {
        if (!box[i].isFinish) {
          box[i].ischecked = false;
          that.data.finishbox = [];
        }
      }
    }
    that.setData({
      finishList: that.data.finishList,
      finishall: !that.data.finishall,
      finishbox: that.data.finishbox
    })
  },
  finishgroup: function (e) {//签退单选项
    const that = this;
    that.data.finishbox = [];
    let box = that.data.finishList;
    let selectbox = e.detail.value;
    if (selectbox.length == box.length) {
      for (let i = 0; i < selectbox.length; i++) {
        if (!box[i].isFinish) {
          box[selectbox[i]].ischecked = true;
          that.data.finishbox.push({
            "userid": box[i].userid,
            "jobid": that.data.jobid
          })
        }
      }
      that.setData({
        finishList: that.data.finishList,
        finishall: !that.data.finishall,
      })
    } else {
      for (let i = 0; i < box.length; i++) {
        if (!box[i].isFinish) {
          box[i].ischecked = false;
        }
      }
      for (let i = 0; i < selectbox.length; i++) {
        if (!box[selectbox[i]].isFinish) {
          box[selectbox[i]].ischecked = true;
          that.data.finishbox.push({
            "userid": box[selectbox[i]].userid,
            "jobid": that.data.jobid
          })
        }
      }
      that.setData({
        finishList: that.data.finishList,
        finishall: false,
      })
    }
  },
  finishThis: function (e) {//单个签退
    const that = this;
    let index = e.currentTarget.dataset.index;
    let box = that.data.finishList;
    console.log(box[index])
    console.log("这单个开始签退")
  },
  finishcommit: function () {//一键提交签退
    const that = this;
    console.log(that.data.finishbox)
  }
})